package com.milk.controller.admin;

import com.milk.constant.JwtClaimsConstant;
import com.milk.dto.CommonPageQueryDTO;
import com.milk.dto.EmployeeDTO;
import com.milk.dto.EmployeeLoginDTO;
import com.milk.entity.Employee;
import com.milk.properties.JwtProperties;
import com.milk.result.PageResult;
import com.milk.result.Result;
import com.milk.service.EmployeeService;
import com.milk.utils.JwtUtil;
import com.milk.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@CrossOrigin
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 修改密码
     * @param dto
     * @return
     */
    @PostMapping("/editPassword")
    public Result<String > editPassword(@RequestBody EmployeeDTO dto){
        log.info("修改的密码 = {}",dto);
        int i = employeeService.editPassword(dto);
        if (i==1) return Result.success("修改成功");
        else return Result.error("修改失败");
    }

    /**
     * 新增员工
     * @param dto
     * @return
     */
    @PutMapping("/insert")
    public Result insert(@RequestBody Employee dto){
        log.info("新增员工 = {}",dto);
        employeeService.insert(dto);
        return Result.success();
    }

    /**
     * 更新员工信息
     * @param dto
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Employee dto){
        log.info("修改员工信息 = {}",dto);
        employeeService.update(dto);
        return Result.success();
    }

    /**
     * 删除员工
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){
        log.info("删除的员工ids = {}",ids);
        employeeService.deleteByIds(ids);
        return Result.success();
    }

    /**
     * 分页查询
     * @return
     */
    @GetMapping
    public Result<PageResult> selectAll(CommonPageQueryDTO dto){
        return Result.success(employeeService.selectAll(dto));
    }

    /**
     * 查询信息
     * @param
     * @return
     */
    @GetMapping("/getInfo")
    @CrossOrigin
    public Result<Employee> adminInfo(){
        Employee employee = employeeService.getEmployeeInfo();
        return Result.success(employee);
    }
}
